package com.gc.web.controller.api;

import com.gc.common.constant.Constants;
import com.gc.framework.web.base.ApiBaseController;
import com.gc.system.domain.SysNotice;
import com.gc.system.service.ISysNoticeService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 公告 Controller
 *
 * @author liujinteng
 * @date 2020-12-01
 */
@CrossOrigin
@RestController
@RequestMapping("/api/sysNotice")
public class ApiSysNoticeController extends ApiBaseController {

    @Autowired
    private ISysNoticeService noticeService;

    /**
     * 公告列表
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/selectNoticeList")
    public Map<String, Object> selectNoticeList(Integer pageNum, Integer pageSize) {
        startPage(pageNum, pageSize, "create_time DESC");
        List<SysNotice> sysNoticeList = noticeService.selectNoticeList(new SysNotice(){{
            setNoticeType("2");
        }});
        Map<String, Object> mapReturn = new HashMap<>();
        mapReturn.put("pageNum", pageNum);
        mapReturn.put("list", sysNoticeList);
        mapReturn.put("total", new PageInfo<>(sysNoticeList).getTotal());
        return success(sysNoticeList);
    }

    /**
     * 公告列表详情
     *
     * @return
     */
    @GetMapping("/selectNoticeInfo")
    public Map<String, Object> selectNoticeList(Long noticeId) {
        if (noticeId == null){
            return error(Constants.UNVERIFY,"参数有误:noticeId为空");
        }
        SysNotice sysNotice = noticeService.selectNoticeById(noticeId);
        if (sysNotice == null){
            return error(Constants.UNVERIFY,"未查找公告数据");
        }
        sysNotice.setBrowseNum(sysNotice.getBrowseNum()+1);
        noticeService.updateNotice(sysNotice);
        return success(sysNotice);
    }


}
